package com.zytech.zspider.engine;

import com.zytech.modules.lang.Lookups;
import com.zytech.zspider.engine.spi.SpiderEngineFactory;
import com.zytech.zspider.engine.spi.SpiderEngineFactoryNotFoundException;

/**
 * @author siuming
 */
public final class SpiderEngines {

    private static final SpiderEngines ME = new SpiderEngines();
    private static final SpiderEngineFactory DEFAULT_ENGINE_FACTORY = new SpiderEngineFactory() {
        @Override
        public SpiderEngine create() {
            return new DefaultSpiderEngine();
        }
    };

    /**
     * @return
     */
    public static SpiderEngines me() {
        return ME;
    }

    /**
     * @return
     */
    public SpiderEngine usingDefault() {
        return DEFAULT_ENGINE_FACTORY.create();
    }

    /**
     * @return
     */
    public SpiderEngine usingCustom() {
        SpiderEngineFactory engineFactory = Lookups.lookup(SpiderEngineFactory.class);
        if (null == engineFactory) {
            throw new SpiderEngineFactoryNotFoundException("Not found SpiderEngineFactory spi.");
        }
        return engineFactory.create();
    }

    private SpiderEngines() {
    }
}
